package cn.azzhu.springdemo.service.impl;

import cn.azzhu.springdemo.bean.Phone;
import cn.azzhu.springdemo.mapper.PhoneMapper;
import cn.azzhu.springdemo.service.PhoneService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author azzhu
 * @create 2020-08-22 14:36:22
 */
@Service
public class PhoneServiceImpl implements PhoneService {

    @Autowired
    private PhoneMapper phoneMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public PageInfo<Phone> getPage(Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //查询
        final List<Phone> list = phoneMapper.getAll();

        return new PageInfo<Phone>(list);
    }

    @Override
    public Phone getById(Integer id) {
        redisTemplate.opsForValue().set("phone_key",phoneMapper.getPhoneById(id).toString(),1, TimeUnit.MINUTES);
        //redisTemplate.opsForValue().get()
        return phoneMapper.getPhoneById(id);
    }
}
